# coding=utf-8

import hiai

voc_class_names = (
    'aeroplane', 'bicycle', 'bird', 'boat',
    'bottle', 'bus', 'car', 'cat', 'chair',
    'cow', 'diningtable', 'dog', 'horse',
    'motorbike', 'person', 'pottedplant',
    'sheep', 'sofa', 'train', 'tvmonitor')
num_class = len(voc_class_names)
index_to_class = dict(zip(range(num_class), voc_class_names))

label_2_num_txt = './utils/coco_label_2_num.txt'
with open(label_2_num_txt, 'r') as f:
    lines = f.readlines()
coco_class_names = []
for line in lines:
    coco_class_names.append(line.strip())

coco_num_class = len(coco_class_names)
coco_index_to_class = dict(zip(range(coco_num_class), coco_class_names))

def CreateGraph(model, modelInWidth, modelInHeight, dvppInWidth, dvppInHeight):
    myGraph = hiai.hiai._global_default_graph_stack.get_default_graph()
    if myGraph is None:
        print('get defaule graph failed')
        return None
    print('dvppwidth %d, dvppheight %d' % (dvppInWidth, dvppInHeight))

    cropConfig = hiai.CropConfig(0, 0, dvppInWidth, dvppInHeight)
    print('cropConfig ', cropConfig)
    resizeConfig = hiai.ResizeConfig(modelInWidth, modelInHeight)
    print('resizeConfig ', resizeConfig)

    nntensorList = hiai.NNTensorList()
    print('nntensorList', nntensorList)

    resultCrop = hiai.crop(nntensorList, cropConfig)
    print('resultCrop', resultCrop)

    resultResize = hiai.resize(resultCrop, resizeConfig)
    print('resultResize', resultResize)

    resultInference = hiai.inference(resultResize, model, None)
    print('resultInference', resultInference)

    if (hiai.HiaiPythonStatust.HIAI_PYTHON_OK == myGraph.create_graph()):
        print('create graph ok !!!!')
        return myGraph
    else:
        print('create graph failed, please check Davinc log.')
        return None


def CreateGraphWithoutDVPP(model):
    # print(model
    myGraph = hiai.hiai._global_default_graph_stack.get_default_graph()
    # print(myGraph
    if myGraph is None:
        print('get defaule graph failed')
        return None

    nntensorList = hiai.NNTensorList()
    # print(nntensorList

    resultInference = hiai.inference(nntensorList, model, None)
    # print(nntensorList
    print(hiai.HiaiPythonStatust.HIAI_PYTHON_OK)
    # print(myGraph.create_graph()

    if (hiai.HiaiPythonStatust.HIAI_PYTHON_OK == myGraph.create_graph()):
        print('create graph ok !!!!')
        return myGraph
    else:
        print('create graph failed, please check Davinc log.')
        return None


def GraphInference(graphHandle, inputTensorList):
    if not isinstance(graphHandle, hiai.Graph):
        print("graphHandle is not Graph object")
        return None

    resultList = graphHandle.proc(inputTensorList)
    return resultList